package org.brisling.sysman.service;

import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.sysman.domain.UserRoles;
import org.brisling.sysman.repository.UserRoleRepo;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/userRole")
/**
 * 用戶角色关联后台服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class UserRoleService extends BaseSysmanService<UserRoles>{
		
	@Autowired
	private UserRoleRepo baseRepo;
	

	public UserRoleService(){
		this.moduleName = ConstSysParam.MODULE_USER_ROLE;
	}
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<UserRoles> getUserListDomain(@RequestParam(value="deptid",required=false) String deptid,
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<UserRoles> getUserList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 根据用户编码查询用户角色
	 * @param userid
	 * @return
	 */
	@RequestMapping("/findRoleByUserid.action")
	public List<UserRoles> findByUserid(@RequestParam(value="userid",required=true) Long userid){
		if(userid!=null)
			return baseRepo.findByUserid(userid);
		
		return null;
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 删除组织节点
	 * @param id
	 */
	@RequestMapping("/save.action")
	public String save(
			@RequestParam(value="userid",required=false)Long userid,
			@RequestParam(value="roleid",required=false)String roleid,
			@RequestBody String body){
		
		if(userid==null){
			return "未选择用户 ";
		}
		StringBuilder sqlBuild = new StringBuilder();		
		sqlBuild.append("DELETE FROM %1$s WHERE userid = %2$d");
		
		String sql = String.format(sqlBuild.toString(), this.getTableName(UserRoles.class),userid);
		
		executeNativeDelOrUpdate(sql);
		if((roleid!=null)&&(roleid.length()>0)){
			String[] _roleArr = roleid.split(",");
			if((_roleArr!=null)&&(_roleArr.length>0)){
				for(int i=0;i<_roleArr.length;i++){
					UserRoles ur = new UserRoles();
					ur.setUserid(userid);
					ur.setRoleid(Long.parseLong(_roleArr[i]));
					super.save(ur);
				}
			}
		}
		
		;
		return "200";
	}
	
	
}
